﻿using UnityModule;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace UnityModule
{
    public sealed partial class ResourceComponent
    {
        private static Dictionary<string, Font> s_dicFont = new Dictionary<string, Font>();//字体
        private string[] fontNames = { /*"fzcyjt", "font_baoji", "font_hurt", "JZYT"*/ };
        private static Dictionary<string, Shader> s_dicShaders = new Dictionary<string, Shader>();

        //预加载
        public void LoadPrelaodRes(Action onPreloadFinish)
        {
            m_onPreloadFinish = onPreloadFinish;
            PreloadFont();
        }

        private int m_initFontCout = 0;
        private void PreloadFont()
        {
            //Debug.Log("ResourceComponent PreloadFont");
            m_initFontCout = 0;
            ++m_initFontCout;
            for (int i = 0; i < fontNames.Length; i++)
            {
                PreloadFont(fontNames[i]);
            }

            OnFontLoaded();
        }

        private void PreloadFont(string fontName)
        {
            //Debug.Log("ResourceComponent PreloadFont" + fontName);
            ++m_initFontCout;            
        }

        private void OnFontLoaded()
        {
            //Debug.Log("ResourceComponent OnFontLoaded");
            --m_initFontCout;
            if (m_initFontCout <= 0)
            {
                PreLoadMaterialAndShader();
            }
        }

        private void PreLoadMaterialAndShader()
        {
            //Debug.Log("ResourceComponent PreLoadMaterialAndShader");
#if UNITY_EDITOR
            if (UpdateSettings.GameSetting.emResType == ResourceMode.EditorLocalRes)
            {
                OnPreloadComplete();
                return;
            }
#endif
            AssetBundleCom.GetBundle("shaders.shader", (ab) =>
            {
                if (null != ab)
                {
                    Shader[] shaders = ab.assetBundle.LoadAllAssets<Shader>();
                    for (int i = 0; i < shaders.Length; i++)
                    {
                        s_dicShaders[shaders[i].name] = shaders[i];
                    }
                }

                OnPreloadComplete();
            }, LoadOptions.DontAutoUnload);
        }

        public static Font GetFont(string fontname)
        {
            Font temp = null;
            s_dicFont.TryGetValue(fontname, out temp);
            return temp;
        }

        /// <summary>
        /// 获取加载的shader
        /// </summary>
        /// <param name="shaderName"></param>
        /// <returns></returns>
        public static Shader GetNewestShader(string shaderName)
        {
            Shader tempShader = null;
            if (!s_dicShaders.TryGetValue(shaderName, out tempShader))
            {
#if !UNITY_EDITOR
                Debug.LogError("获取shader失败  " + shaderName);
#endif
            }
            return tempShader;
        }

        private void OnPreloadComplete()
        {
            m_onPreloadFinish?.Invoke();
        }
    }
}
